<template>
  <div>
    <div class="ray" :style="{ width: rayWidth + 'px' }">
      <div
        class="ray-segment"
        v-for="(segment, index) in segments"
        :key="index"
        :style="{ width: segment.width + '%', left: segment.left + '%' }"
      ></div>
    </div>
    <div>
      <button @click="addSegment">添加</button>
      <input type="number" v-model="segmentCount" @input="updateSegments" />
      <button @click="updateSegments">份数</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rayWidth: 400,
      segmentCount: 1,
      segments: [{ width: 100, left: 0 }],
    };
  },
  methods: {
    addSegment() {
      this.segments.push({ width: 0, left: 100 });
    },
    updateSegments() {
      const segmentWidth = 100 / this.segmentCount;
      this.segments.forEach((segment, index) => {
        segment.width = segmentWidth;
        segment.left = segmentWidth * index;
      });
    },
  },
};
</script>

<style>
.ray {
  height: 10px;
  background-color: #ccc;
  position: relative;
}

.ray-segment {
  height: 100%;
  background-color: #f00;
  position: absolute;
  top: 0;
}
</style>
